package com.newhis.listener;

import com.newhis.event.LoginEvent;
import com.newhis.event.LoginMessageNoticeEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class LoginEventListener {
    private static final Logger log = LoggerFactory.getLogger(LoginEventListener.class);
    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;
    @Async
    @EventListener
    public void handleLoginEvent(LoginEvent loginEvent){
        log.info("登录成功后的事件发布，用户名：{}", loginEvent.getUsername());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        applicationEventPublisher.publishEvent(new LoginMessageNoticeEvent(this, loginEvent.getUsername()));
    }
}
